﻿using System.ComponentModel;
using System.Linq;

namespace Willow.Extensions
{
    public static class CollectionsExtensions
    {

        /// <summary>
        /// 把IQueryable[T]集合按指定属性与排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="propertyName">排序属性名</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns>
        /// 排序后的数据集
        /// </returns>
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName,
            ListSortDirection sortDirection = ListSortDirection.Ascending)
        {
            return CollenctionsHelper<T>.OrderBy(source, propertyName, sortDirection);
        }

        /// <summary>
        /// 把IQueryable[T]集合按指定属性排序条件进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="sortCondition">列表属性排序条件</param>
        /// <returns></returns>
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, PropertySortCondition sortCondition)
        {
            return source.OrderBy(sortCondition.PropertyName, sortCondition.ListSortDirection);
        }

        /// <summary>
        /// 把IOrderedQueryable[T]集合继续按指定属性排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="propertyName">排序属性名</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns></returns>
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propertyName,
            ListSortDirection sortDirection = ListSortDirection.Ascending)
        {
            return CollenctionsHelper<T>.ThenBy(source, propertyName, sortDirection);
        }

        /// <summary>
        /// 把IOrderedQueryable[T]集合继续指定属性排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="sortCondition">列表属性排序条件</param>
        /// <returns></returns>
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, PropertySortCondition sortCondition)
        {
            return source.ThenBy(sortCondition.PropertyName, sortCondition.ListSortDirection);
        }

        /// <summary>
        /// 从指定IQueryable[T]集合 中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="total">输出符合条件的总记录数</param>
        /// <param name="sortConditions">排序条件集合</param>
        /// <returns></returns>
        public static IQueryable<T> SelectToPage<T>(this IQueryable<T> source, int pageIndex, int pageSize,
            out int total, params PropertySortCondition[] sortConditions)
        {

            total = source.Count();

            int count = 0;
            IOrderedQueryable<T> orderSource = null;
            foreach (PropertySortCondition sortCondition in sortConditions)
            {
                orderSource = count == 0
                    ? source.OrderBy(sortCondition.PropertyName, sortCondition.ListSortDirection)
                    : orderSource.ThenBy(sortCondition.PropertyName, sortCondition.ListSortDirection);
                count++;
            }

            if (sortConditions.Length > 0)
            {
                source = orderSource;
            }

            return source != null
                ? source.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<T>().AsQueryable();
        }

    }



}
